package com.agua.enturno.vista.paginas.prestador;

import com.agua.enturno.vista.componentes.AgendaChoiceRenderer;
import com.agua.enturno.vista.dto.AgendaDTO;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import java.util.List;

public class AgendasDeServicioPanel extends Panel {

	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("unused")
	private AgendaDTO agenda;
	private AgendaFullPanel agendaFullPanel;
	
	private IModel<List<AgendaDTO>> model;

	public AgendasDeServicioPanel(String id, IModel<List<AgendaDTO>> model) {
		super(id, model);
		setOutputMarkupId(true);
		this.model = model;
		
		agenda = model.getObject().get(0);
				
        @SuppressWarnings("rawtypes")
		Form form = new Form("select-agenda-form");
        add(form);
        
        DropDownChoice<AgendaDTO> selectAgendas = new DropDownChoice<AgendaDTO>("agendas", new PropertyModel<AgendaDTO>(this, "agenda"), model, new AgendaChoiceRenderer());
        selectAgendas.add(new AjaxFormComponentUpdatingBehavior("onchange") {
            private static final long serialVersionUID = 1L;

			protected void onUpdate(AjaxRequestTarget target) {
				target.add(agendaFullPanel);
            }
        });
        form.add(selectAgendas);
		
		agendaFullPanel = new AgendaFullPanel("agenda-full", new PropertyModel<AgendaDTO>(this, "agenda"));
		add(agendaFullPanel);
	}
	
	@Override
	protected void onBeforeRender() {
		agenda = model.getObject().get(0);
		super.onBeforeRender();
	}

}
